package com.css.zfzx.sjcj.modules.drill.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.bpm.platform.utils.UUIDGenerator;
import com.css.zfzx.sjcj.common.utils.BeanUtil;
import com.css.zfzx.sjcj.common.utils.ServerUtil;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY08Entity;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY18Entity;
import com.css.zfzx.sjcj.modules.drill.Entity.vo.QhY18Vo;
import com.css.zfzx.sjcj.modules.drill.repository.QhY18NativeRepository;
import com.css.zfzx.sjcj.modules.drill.repository.QhY18Repository;
import com.css.zfzx.sjcj.modules.drill.service.QhService;
import com.css.zfzx.sjcj.modules.drill.service.QhY18Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Li Helei
 * @date 2021/1/16 10:44
 */
@Slf4j
@Service
public class QhY18ServiceImpl implements QhY18Service {

	@Autowired
    private QhY18Repository qhY18Repository;
    @Autowired
    private QhY18NativeRepository qhY18NativeRepository;
    @Autowired
    private QhService qhService;
    @Override
    public JSONObject queryQhY18s(String drillId, int curPage, int pageSize) {
        Page<QhY18Entity> qhY18Page = this.qhY18NativeRepository.queryQhY18s(drillId, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(qhY18Page);
        return jsonObject;
    }

    @Override
    public QhY18Entity findDrillId(String drillId) {
        return this.qhY18Repository.findByDrillIdAndIsValid(drillId, "1");
    }

    /*@Override
    public JSONObject queryQhY18s(QhY18QueryParams queryParams) {
        List<QhY18Entity> qhY18List = this.qhY18NativeRepository.listQhY18s(queryParams);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", qhY18List);
        return jsonObject;
    }*/


    @Override
    public QhY18Entity getQhY18(String id) {
        QhY18Entity qhY18 = this.qhY18Repository.findByIdAndIsValid(id,"1");
         return qhY18;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhY18Entity saveQhY18(QhY18Entity qhY18) {
        try {
            String uuid = UUIDGenerator.getUUID();
            qhY18.setId(uuid);
            qhY18.setIsValid("1");
            this.qhY18Repository.save(qhY18);
            qhService.update(qhY18);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qhY18;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhY18Entity updateQhY18(QhY18Entity qhY18){
        try {
            String id = qhY18.getId();
            QhY18Entity one = qhY18Repository.findById(id).get();
            BeanUtil.copyPropertiesIgnoreNull(qhY18, one);
            this.qhY18Repository.save(one);
            qhService.update(one);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return qhY18;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteQhY18s(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        List<QhY18Entity> qhY18List = this.qhY18Repository.findAllById(idList);
        if(!PlatformObjectUtils.isEmpty(qhY18List) && qhY18List.size() > 0) {
            for(QhY18Entity qhY18 : qhY18List) {
                qhY18.setIsValid("0");
                this.qhY18Repository.save(qhY18);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitQhY18(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        List<QhY18Entity> qhY18List = this.qhY18Repository.findAllById(idList);
        if(!PlatformObjectUtils.isEmpty(qhY18List) && qhY18List.size() > 0) {
            for(QhY18Entity qhY18 : qhY18List) {
                this.qhY18Repository.save(qhY18);
            }
        }
    }

    @Override
    public QhY18Vo dataQualityCheck(String drillId) {
        JSONObject res = new JSONObject();
        QhY18Vo qhY18Vo=null;
        try {
            QhY18Entity qhY18=qhY18Repository.findByDrillIdAndIsValid(drillId,"1" );
            if(!PlatformObjectUtils.isEmpty(qhY18)) {
                qhY18Vo=new QhY18Vo();
                qhY18Vo.setId(qhY18.getId());
                //检查字段值不为空
                StringBuilder errorMsg = new StringBuilder();
                if(StringUtils.isEmpty(qhY18.getSiteType())){
                    ServerUtil.addErrorMsg(errorMsg, "场地类型", "为空");
                }
                if(errorMsg.length()>0|| StringUtils.isNotEmpty(errorMsg.toString())) {
                    qhY18Vo.setQualityOpinion("Y18的"+errorMsg.toString());
                    qhY18Vo.setDataState("1");
                    return qhY18Vo;
                }
                //检查字段属性
                String siteType=qhY18.getSiteType();

                if (!(siteType.equals("I0")||siteType.equals("I1")||siteType.equals("II")||siteType.equals("III")||siteType.equals("IV"))){
                    ServerUtil.addErrorMsg(errorMsg, "场地类别", "不符合规范（I0,I1,II,III,IV）");
                }

                String strErrorMsg = errorMsg.toString();
                if(!strErrorMsg.isEmpty()) {
                    qhY18Vo.setQualityOpinion("Y18的"+strErrorMsg);
                    qhY18Vo.setDataState("1");
                } else {
                    qhY18Vo.setQualityOpinion("系统自动质检通过");
                    qhY18Vo.setDataState("2");
                }
            }
        } catch (Exception e){
            log.error(e.getMessage());
        }
        return qhY18Vo;
    }

    @Override
    public List<String> getDrillIds(String siteType) {
        ArrayList<String> list = new ArrayList<>();
        List<QhY18Entity> all = qhY18Repository.findAllBySiteTypeAndIsValid(siteType, "1");
        all.stream().forEach(qhY18Entity -> {
            list.add(qhY18Entity.getDrillId());
        });
        return list;
    }

}
